home *** CD-ROM | disk | FTP | other *** search
- Path: dawn.mmm.com!news
- From: kjhopps@mmm.com (Kevin J Hopps)
- Newsgroups: comp.lang.c++
- Subject: Re: Passing 2-dimensional Array
- Date: 15 Feb 1996 16:32:06 GMT
- Organization: 3M - St. Paul, MN 55144-1000 US
- Message-ID: <4fvn66$doe@dawn.mmm.com>
- References: <4fo6a9$hvt@tali.UCHSC.edu> <311D100B.7908@gsfc.nasa.gov>
- Reply-To: kjhopps@mmm.com
- X-Newsreader: TIN [version 1.2 PL2]
-
- Dirk Broer (Dirk.Broer@gsfc.nasa.gov) wrote:
- > Jan Ingebrigtsen wrote:
- > >
- > > How do I pass a pointer to a 2 dimensional array in function calls in C?
- > >
- > > I have tried the following but have had no success:
- > >
- > > main()
- > > {
- > > double (*TwoDim)[Y] = new double [X][Y];
- > > foo( TwoDim, X, Y )
- > > }
- > >
- > > void foo( double *TwoDim[], int x, int y )
- > > {
- > > // process TwoDim here
- > > }
- > >
- > > I will get the following error message:
- > >
- > > cannot convert paramter 1 from 'double (__near *)[Y]' to 'double
- > > __near*'
- > >
-
-
- > A 2 dimensional array is created in C as a pointer to a pointer. So if
- > you create an NxM array C creats an N array of pointers to other arrays.
- > Additionally it creates N arrays with M values in it. Try defining the
- > function as:
-
- This is not correct. The statement
-
- double (*TwoDim)[Y] = new double[X][Y];
-
- allocates X*Y doubles and makes TwoDim point to the first one. No
- pointers are allocated by this call to new.
-
- > func( double **TwoDim );
-
- This is equivalent to
- func(double* TwoDim[]);
-
- > TwoDim can them be accessed normally using TwoDim[x][y];
-
- > Creating TwoDim might not be as easy through.
-
- It is easy if you do not want to allocate an array of pointers to
- double and use it as a 2D array of double.
-
- > > double (*TwoDim)[Y] = new double [X][Y];
-
- > I'm surprised this worked. The way I read it you are creating XxY doubles
- > and returning a pointer to the first one. When you try to access it via
- > TwoDim[x][y] the compiler first takes the pointer TwoDim and offsets 'x' -
- > this will give you the xth item you've created. It then takes a further
- > offset of [y] and returns the value at [x+y]; This is clearly wrong.
-
- > Am I missing something?
-
- The compiler will offset x*Y+y doubles.
-
- > double (*TwoDim)[y] is of course an array of y pointers to doubles.
-
- > > void foo( double *TwoDim[], int x, int y )
-
- > double *TwoDim[] is a pointer to an array of doubles....
-
- > Put brackets around *TwoDim and it should compile - but it still may not
- > work right. Double check what happens when you do new[x][y];
-
- Correct, it will not work right unless you also put a Y in the brackets:
- void foo( double (*TwoDim)[Y], int x, int y )
- --
- Kevin J. Hopps e-mail: kjhopps@mmm.com
- 3M Company phone: (612) 737-4643
- 3M Center, Bldg. 235-2D-57 fax: (612) 737-2700
- St. Paul, MN 55144-1000 Opinions are my own. I don't speak for 3M.
- But 3M speaks for me -- I did not write the following line:
-
- Opinions expressed herein are my own and may not represent those of 3M.
-